package com.example.listener;


import com.example.common.util.SpringContextUtil;
import com.example.service.IActFlowCustomService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.runtime.ProcessInstance;

/**
 * 处理审批驳回逻辑
 */
public class TaskListener implements org.activiti.engine.delegate.TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        Boolean taskResult = delegateTask.getVariableLocal("taskResult", Boolean.class);
        if (!taskResult) {
            RuntimeService runtimeService = SpringContextUtil.getBean(RuntimeService.class);
            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(delegateTask.getProcessInstanceId()).singleResult();
            String businessKey = processInstance.getBusinessKey();
            String[] split = businessKey.split(":");
            String formKey=split[0];
            String id=split[1]+":"+split[2];
            String beanName=formKey+"ServiceImpl";
            IActFlowCustomService customService = (IActFlowCustomService)SpringContextUtil.getBean(beanName);
            customService.endRunTask(id,false,delegateTask.getProcessDefinitionId(), "");
        }
    }
}
